package com.dharma.mydiet.view;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.DAOFood;
import com.dharma.mydiet.storage.TransactionalFood;
import com.dharma.mydiet.utility.Utils;
import com.google.analytics.tracking.android.EasyTracker;

/**
 * @author Jose
 * 
 */
public class InfoFoodsAcrtivity extends Activity {

    ArrayAdapter<CharSequence> adapterTypeFood;
    private ArrayList<TransactionalFood> foods;
    private Spinner foodType;

    private TextView calorias;
    private TextView carbohidratos;
    private TextView proteinas;
    private TextView lipidos;
    private TextView azucar;
    private TextView colesterol;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.info_alimento_activity);
	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());
	ActionBar ab = getActionBar();
	ab.setSubtitle("Informacion Alimentos");
	calorias = (TextView) findViewById(R.id.tcalorias);
	carbohidratos = (TextView) findViewById(R.id.tcarbohidratos);
	proteinas = (TextView) findViewById(R.id.tproteinas);
	lipidos = (TextView) findViewById(R.id.tlipidos);
	azucar = (TextView) findViewById(R.id.tsugar);
	colesterol = (TextView) findViewById(R.id.tcolesterol);

	Button newFood = (Button) findViewById(R.id.buttonNewFood);
	newFood.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View arg0) {
		Intent intent = new Intent(getApplicationContext(), NewFoodActivity.class);
		startActivity(intent);
	    }
	});
	foodType = (Spinner) findViewById(R.id.foods);
	adapterTypeFood = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
	adapterTypeFood.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	foods = DAOFood.getInstance().getFoods();
	for (int i = 0; i < foods.size(); i++) {
	    adapterTypeFood.add(((TransactionalFood) foods.get(i)).getName());
	}
	foodType.setAdapter(adapterTypeFood);

	foodType.setOnItemSelectedListener(new OnItemSelectedListener() {

	    @Override
	    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		if (arg2 > 0) {
		    setValues(foods.get(arg2));
		}
	    }

	    @Override
	    public void onNothingSelected(AdapterView<?> arg0) {

	    }

	});
    }

    private void setValues(TransactionalFood food) {
	calorias.setText("" + food.getCalories());
	carbohidratos.setText("" + food.getPorcentualCarbohidraths());
	proteinas.setText("" + food.getPorcentualProteins());
	lipidos.setText("" + food.getPorcentualLipids());
	azucar.setText("" + food.getShugar());
	colesterol.setText("" + food.getColesterol());
    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
